﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

[Serializable]
public class Card
{    
    //卡牌属性
    public string img = "";
    public string id = "";
    public string name = "";
    private int levelUpLimit = 5;//等级上限
    public int level = 1;
    public GMConfig.ElementType elementType = GMConfig.ElementType.UnPoint;

    public GMConfig.CardType type = GMConfig.CardType.Common;
    public List<GMConfig.CardRoleType> roles = new List<GMConfig.CardRoleType>();
    private GMConfig.CardSkillType _skillType;
    public GMConfig.CardSkillType skillType {
        set {
            _skillType = value;
            ResetSkill();
        }
        get {
            return _skillType;
        }
    }
    public List<SkillBase> skill = new List<SkillBase>();

    public Card()
    {
        skillType = GMConfig.CardSkillType.Trick;
    }
    /// <summary>
    /// 获得卡牌名称
    /// </summary>
    /// <returns></returns>
    public string GetCardName()
    {
        return LTLocalization.GetText(name);
    }
    /// <summary>
    /// 重置技能
    /// </summary>
    private void ResetSkill()
    {
        skill.Clear();
        for (int i = 0; i < levelUpLimit; i++)
        {
            if (skillType == GMConfig.CardSkillType.Skill)
            {
                skill.Add(new CardSkill());
            }
            else
            {
                skill.Add(new CardTrick());
            }
        }
    }
    /// <summary>
    /// 使用角色发生改变
    /// </summary>
    /// <param name="roleType"></param>
    public void RolesChange(GMConfig.CardRoleType roleType)
    {
        if (roles.Contains(roleType)) roles.Remove(roleType);
        else roles.Add(roleType);
    }
    /// <summary>
    /// 激活随机属性
    /// </summary>
    public void ActiveRandProperty()
    {
        //确定随机属性
        for(int k = 0;k < skill.Count;k++){
            var hasPropertyList = GetHasPropertyList(k);
            for (int j = 0; j < hasPropertyList.Count; j++)
            {
                hasPropertyList[j].val = Utils.GetRandVal(hasPropertyList[j].val.ToString());
                Debug.Log(hasPropertyList[j].GetDiscript() + "   " + hasPropertyList[j].GetVal()[0].ToString());
            }
        }
    }
    /// <summary>
    /// 获得所有属性
    /// </summary>
    /// <param name="level"></param>
    /// <returns></returns>
    public List<PropertyValue<object>> GetHasPropertyList(int level)
    {
        var hasPropertyList = new List<PropertyValue<object>>();
        if (skill[level] == null) return hasPropertyList;
        var levelSkill = skill[level];
        //属性大类
        for (int i = 0; i < levelSkill.propertyList.Count; i++)
        {
            //属性小类
            for (int j = 0; j < levelSkill.propertyList[i].val.propertyList.Count; j++)
            {
                var pro = levelSkill.propertyList[i].val.propertyList[j];
                //不为0说明具备
                if (pro.val.ToString() != "0") hasPropertyList.Add(pro);
            }
        }
        return hasPropertyList;
    }
    /// <summary>
    /// 获得属性描述
    /// </summary>
    /// <param name="level"></param>
    /// <returns></returns>
    public string GetPropertyDiscript(int level)
    {
        if (skill[level] == null) return "";
        var levelSkill = skill[level];
        string discript = "";
        bool isAdd = false;
        //属性大类
        for (int i = 0; i < levelSkill.propertyList.Count; i++)
        {
            isAdd = false;
            //属性小类
            for (int j = 0; j < levelSkill.propertyList[i].val.propertyList.Count; j++)
            {
                var pro = levelSkill.propertyList[i].val.propertyList[j];
                //不为0说明具备
                if (pro.val.ToString() != "0")
                {
                    if(levelSkill.propertyList[i].GetDiscript() == "需要") continue;
                    //大类描述
                    if (!isAdd)
                    {
                        isAdd = true;
                        if (discript != "") discript += ",";
                        discript += levelSkill.propertyList[i].GetDiscript() + ":";
                    }
                    else
                    {
                        if (discript != "") discript += ",";
                    }
                    discript += pro.GetDiscript();
                }
            }
        }
        return discript;
    }
    /// <summary>
    /// 获得需要属性描述
    /// </summary>
    /// <param name="level"></param>
    /// <returns></returns>
    public string GetNeedDiscript(int level)
    {
        if (skill[level] == null) return "";
        var levelSkill = skill[level];
        string discript = "";
        bool isAdd = false;
        //属性大类
        for (int i = 0; i < levelSkill.propertyList.Count; i++)
        {
            isAdd = false;
            //属性小类
            for (int j = 0; j < levelSkill.propertyList[i].val.propertyList.Count; j++)
            {
                var pro = levelSkill.propertyList[i].val.propertyList[j];
                //不为0说明具备
                if (pro.val.ToString() != "0")
                {
                    if(levelSkill.propertyList[i].GetDiscript() != "需要") continue;
                    //大类描述
                    if (!isAdd)
                    {
                        isAdd = true;
                        if (discript != "") discript += ",";
                        discript += levelSkill.propertyList[i].GetDiscript() + ":";
                    }
                    else
                    {
                        if (discript != "") discript += ",";
                    }
                    discript += pro.GetDiscript();
                }
            }
        }
        return discript;
    }
    /// <summary>
    /// 深拷贝
    /// 修改对象内容不会互相影响
    /// </summary>
    /// <returns></returns>
    public Card DeepClone()
    {
        using (Stream objectStream = new MemoryStream())
        {
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(objectStream,this);
            objectStream.Seek(0, SeekOrigin.Begin);
            return formatter.Deserialize(objectStream) as Card;
        }
    }
}

